package com.ruoyi.numericalselection.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.ConstantsType;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.numericalselection.domain.GmCategory;
import com.ruoyi.numericalselection.domain.dao.GmTypeDao;
import com.ruoyi.numericalselection.mapper.GmCategoryMapper;
import com.ruoyi.numericalselection.service.IGmCategoryService;
import com.ruoyi.numericalselection.service.IGmInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-29
 */
@Service
public class GmCategoryServiceImpl implements IGmCategoryService {
    @Autowired
    private GmCategoryMapper gmCategoryMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IGmInventoryService iGmInventoryService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public GmCategory selectGmCategoryById(Long id) {
        GmCategory gmCategory = gmCategoryMapper.selectGmCategoryById(id);
        if (gmCategory.getImages() != null && !gmCategory.getImages().isEmpty()) {
            gmCategory.setImgs(Collections.singletonList(gmCategory.getImages()));
        }
        return gmCategory;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param gmCategory 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<GmCategory> selectGmCategoryList(GmCategory gmCategory) {
        List<GmCategory> gmCategories = gmCategoryMapper.selectGmCategoryList(gmCategory);
        return gmCategories;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param gmCategory 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertGmCategory(GmCategory gmCategory) {
        if (gmCategory.getImgs() != null && !gmCategory.getImgs().isEmpty()) {
            gmCategory.setImages(gmCategory.getImgs().get(0));
        }
        return gmCategoryMapper.insertGmCategory(gmCategory);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param gmCategory 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateGmCategory(GmCategory gmCategory) {
        if (!gmCategory.getImgs().isEmpty()) {
            gmCategory.setImages(gmCategory.getImgs().get(0));
        }
        return gmCategoryMapper.updateGmCategory(gmCategory);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteGmCategoryByIds(Long[] ids) {
        return gmCategoryMapper.deleteGmCategoryByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteGmCategoryById(Long id) {
        return gmCategoryMapper.deleteGmCategoryById(id);
    }

    /**
     * 获取游戏信息
     *
     * @return
     */
    @Override
    public List<GmCategory> getGmCategory() {
        List<GmCategory> gmCategory = gmCategoryMapper.getGmCategory();
        List<GmCategory> sortedList = gmCategory.stream()
                .sorted(Comparator.comparing(GmCategory::getSort).reversed())
                .collect(Collectors.toList());
        return sortedList;

    }

    /**
     * 按类型查询
     *
     * @param type
     * @return
     */
    @Override
    public List<GmCategory> getGm(String type) {

        if (ConstantsType.APP_GM_TYPE_POPULAR.equals(type) ||
                ConstantsType.APP_GM_TYPE_End_GAME.equals(type) || ConstantsType.APP_GM_TYPE_MODILE_GAMES.equals(type)) {
            return gmCategoryMapper.getTypeOrderBy(type);
        }
        throw new BaseException("查询而类型错误");
    }

    @Override
    public List<GmCategory> selectAppGmCategoryList() {
        return gmCategoryMapper.selectAppGmCategoryList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateType(GmTypeDao gmTypeDao) {
        GmCategory gmCategory = gmCategoryMapper.selectGmCategoryById(gmTypeDao.getId());
        if (gmCategory == null) {
            throw new BaseException("没查询到库存实体");
        }
        gmCategory.setIsItDisplayed(gmTypeDao.getIsItDisplayed());
        gmCategory.setLogicalDeletion(gmTypeDao.getLogicalDeletion());
        gmCategory.setIsPopular(gmTypeDao.getIsPopular());
        if (gmTypeDao.getLogicalDeletion().equals("0")) {
            int gmCount = iGmInventoryService.selectCount(gmTypeDao.getId());
            if (gmCount > 0) {
                int delect = iGmInventoryService.updateGmIdByDelect(gmTypeDao.getId());
                if (delect ==0){
                    throw  new BaseException("删除关联子集失败");
                }
            }
        }
        return gmCategoryMapper.updateGmCategory(gmCategory);
    }
}
